package com.gu.controller;

import com.boot.exception.DiyException;
import com.gu.entity.weixin.ConstantProperties;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Description: 微信开发者接口类
 *
 * @Autor: Rolin
 * datetime: 06-13-23:05
 */
@Controller
@Api(tags = "微信开发者接口类")
@CrossOrigin
@RequestMapping("/edu/ucenter")
public class WxOpenController {

    @Resource
    private ConstantProperties constantProperties;

    @GetMapping("/test")
    public String test() {
        //向认证服务器发送请求换取access_token
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code" +
                "&scope=snsapi_login" +
                "&state=%s" +
                "#wechat_redirect";
       String  redirectUrl = constantProperties.getRedirect_url();
        try {
            redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new DiyException("20001", e.getMessage());
        }

        String qrcodeUrl = String.format(baseUrl,
                constantProperties.getApp_id(),
                redirectUrl,
                "yuyikao");

        return "redirect:"+baseUrl ;
    }
}
